package view;

import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import controller.Controller;

import view.components.ComponentsFactory;
import view.components.ErrorCatcher;
import view.components.SLogoCanvas;
import view.components.SLogoMenu;
import view.components.SLogoTabManager;

/**
 * Window for SLogo, which contains all view components
 * @author Yin
 *
 */
@SuppressWarnings("serial")
public class SLogoView extends JFrame {
    private Controller myController;
    private SLogoTabManager myTab;
    private static final String WINDOW_SIZE = "SLogoViewSize";
    private static final String WINDOW_ERROR = "SLogoViewError";

    public SLogoView(String title, Controller controller) throws IOException
    {
        super(title);

        List<Integer> size = getSize(WINDOW_SIZE);
        try {
            setPreferredSize(new Dimension(size.get(0), size.get(1)));
        } catch (Exception e) {
            new ErrorCatcher(WINDOW_ERROR);
        }

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        myController = controller;

        // Set the menu bar and add the label to the content pane.
        myTab = new SLogoTabManager(ComponentsFactory.getComponentsMap().get(
                "TabTitle"), myController);
        this.getContentPane().add(myTab);
        this.setJMenuBar(new SLogoMenu(myController, myTab).getMenuBar());

        pack();
        setVisible(true);

    }

    /**
	 * A static method that read component size from resource file
	 * @param str
	 * @return list of integer which represent component's width and height
	 */
    public static List<Integer> getSize(String str) {
        List<Integer> size = new ArrayList<Integer>();
        for (String s : ComponentsFactory.getComponentsMap().get(str)
                .split(" ")) {
            size.add(Integer.parseInt(s));
        }

        return size;
    }

    /**
     * 
     * @return active canvas
     */
    public SLogoCanvas getCanvas() {
        return (SLogoCanvas) myTab.getSelectedComponent();
    }

    /**
     * 
     * @return active Observer
     */
    public Observer getObserver() {
        return myTab.getObserver();
    }
}